# Clean Environment -------------------------------------------------------
rm(list = ls())

# Data --------------------------------------------------------------------
polpan <- readRDS("polpan_subset.rds")
source("figure_design.R")

# Minority yield to majority ------------------------------------------------

# Define a function to preprocess data
preprocess_data <- function(data, renames, birthyear, vote_2015, values) {
  data %>%
    rename(!!!renames, birthyear = birthyear, vote_2015 = vote_2015) %>%
    dplyr::select(all_of(values), birthyear, vote_2015) %>%
    mutate(vote_PiS = case_when(
      vote_2015 == "2" ~ "PiS",
      vote_2015 == "1" ~ "PO",
      vote_2015 %in% 3:9 ~ "other",
      TRUE ~ NA_character_)) %>%
    mutate(across(where(is.numeric), ~ replace(., . < 0, NA))) %>%
    mutate(across(where(is.numeric), ~ replace(., . > 5, NA))) %>%
    mutate(across(where(is.numeric), ~ case_when(
      . == 1 ~ 5,
      . == 2 ~ 4,
      . == 3 ~ 3,
      . == 4 ~ 2,
      . == 5 ~ 1,
      TRUE ~ NA_real_
    )))
}

# Define a function to plot data
plot_data <- function(data, y_label, output_file, plot_colors) {
  data %>% dplyr::select(all_of(values), vote_PiS) %>%
    tidyr::gather(key = "key", value = "value", -vote_PiS) %>%
    group_by(key, vote_PiS) %>%
    summarise(mean = mean(value, na.rm = TRUE),
              cilower = t.test(value)$conf.int[1],
              cihigher = t.test(value)$conf.int[2]) %>%
    drop_na(vote_PiS) -> data
  
  pdf(file = output_file, width = 8, height = 6, onefile = FALSE)
  
  print(ggplot(data, aes(x = as.numeric(key), y = mean, color = vote_PiS)) +
          geom_line(aes(linetype = vote_PiS), size = 1) +
          geom_point(position = position_dodge(0.2)) +
          xlab("Wave") +
          ylab(y_label) +
          theme(legend.title = element_blank()) +
          geom_errorbar(aes(ymin = cilower, ymax = cihigher), width = 0.2,
                        position = position_dodge(0.2)) +
          scale_x_continuous(breaks = as.numeric(values)) +
          ylim(1, 5) +
          scale_color_manual(values = plot_colors))
  
  dev.off()
}

# Data renaming and values
renames_list <- list(
  democracy = c(`1998` = "XM07A", `2003` = "WM0708A", `2008` = "WM0708K", `2013` = "UC02G", `2018` = "TC01P"),
  obeying_law = c(`1998` = "XM07K", `2003` = "WM0708K", `2008` = "VM04F", `2013` = "UC01F", `2018` = "TC01F"),
  elections = c(`1998` = "XM07I", `2003` = "WM0708I", `2008` = "VM04E", `2013` = "UC01I", `2018` = "TC01H"),
  single_party = c(`1998` = "XM06F", `2003` = "WM06F", `2008` = "VM03F", `2013` = "UC01B", `2018` = "TC01A")
)

values <- c("1998", "2003", "2008", "2013", "2018")

# Preprocess data
panel_democracy <- preprocess_data(polpan, renames_list$democracy, "YRBIRTH", "TE08", values)
panel_obeying_law <- preprocess_data(polpan, renames_list$obeying_law, "YRBIRTH", "TE08", values)
panel_elections <- preprocess_data(polpan, renames_list$elections, "YRBIRTH", "TE08", values)
panel_single_party <- preprocess_data(polpan, renames_list$single_party, "YRBIRTH", "TE08", values)

# Plot data
plot_data(panel_democracy, "Democracy Means Minority Yields to Majority", "fig_e1a.pdf", plot_colors)
plot_data(panel_obeying_law, "Good Leader Does not Need to Obey the Law", "fig_e1b.pdf", plot_colors)
plot_data(panel_elections, "Elections Are not Necessary if Leaders Represent Interest of Citizens", "fig_e1c.pdf", plot_colors)
plot_data(panel_single_party, "One Good Political Party Would Make Other Parties Unnecessary", "fig_e1d.pdf", plot_colors)
